package com.tmy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tmy.Dto.Result;
import com.tmy.entity.Dict;
import com.tmy.entity.DictItem;
import com.tmy.mapper.DictItemMapper;
import com.tmy.mapper.DictMapper;
import com.tmy.service.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DictServiceImpl implements DictService {

    @Resource
    private DictMapper dictMapper;
    @Resource
    private DictItemMapper dictItemMapper;


    /**
     * 新增字典分类
     */
    @Override
    public Result addDict(Dict dict){
         Result result =new Result();
         try {
             dictMapper.insert(dict);
             result.setCode(200);
             result.setMsg("字典新增成功");
         }catch (Exception err){
             result.setCode(500);
             result.setMsg(err.getCause());
         }
         return result;
     }
    /**
     * 删除字典分类
     */
    @Override
    public Result deleteDict(Dict dict){
        Result result =new Result();
        try {
            dictMapper.deleteById(dict);
            LambdaQueryWrapper<DictItem> lqw =new LambdaQueryWrapper<>();
            lqw.eq(DictItem::getDictId,dict.getId());
            dictItemMapper.delete(lqw);
            result.setCode(200);
            result.setMsg("字典删除成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 更新字典分类
     */
    @Override
    public Result updateDict(Dict dict){
        Result result =new Result();
        try {
            dictMapper.updateById(dict);
            result.setCode(200);
            result.setMsg("字典更新成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 查询字典分类
     */
    @Override
    public Result queryDict(){
        Result result =new Result();
        try {
            List<Dict> dictList = dictMapper.selectList(null);
            result.setData(dictList);
            result.setCode(200);
            result.setMsg("字典查询成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }


    /**
     * 新增字典值
     */

    @Override
    public Result addDictItem(DictItem dictItem){
        Result result =new Result();
        try {
            dictItemMapper.insert(dictItem);
            result.setCode(200);
            result.setMsg("字典项新增成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 删除字典值
     */
    @Override
    public Result deleteDictItem(DictItem dictItem){
        Result result =new Result();
        try {
            dictItemMapper.deleteById(dictItem);
            result.setCode(200);
            result.setMsg("字典项删除成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 更新字典值
     */
    @Override
    public Result updateDictItem(DictItem dictItem){
        Result result =new Result();
        try {
            dictItemMapper.updateById(dictItem);
            result.setCode(200);
            result.setMsg("字典项更新成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 查询字典值
     */
    @Override
    public Result queryDictItem(Dict dict){
        Result result =new Result();
        try {
            LambdaQueryWrapper<DictItem> lqw =new LambdaQueryWrapper<>();
            lqw.eq(DictItem::getDictId,String.valueOf(dict.getId()));
            List<DictItem> dictItemList = dictItemMapper.selectList(lqw);
            result.setData(dictItemList);
            result.setCode(200);
            result.setMsg("字典项查询成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
}
